package com.hs.panghu.dao.imp;

import com.hs.panghu.dao.ShopDao;
import com.hs.panghu.entity.Shop;
import com.hs.panghu.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ShopDaoImpl implements ShopDao {
    @Override
    public int insertShop(Shop shop) {
        String sql = "insert into shop values(now(),null,null,null,null,?,null,?,null,?,?,null,null,?,?,?,?,null,null,?,?,null,null,null)";
        Object[] object = {
                shop.getShopAddress(),
                shop.getShopCity(),
                shop.getShopDescribe(),
                shop.getShopDistrict(),
                shop.getShopImage(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getShopName(),
                shop.getShopPhone(),
                shop.getShopProvince(),
        };
        return JDBCDruidUtil.update(sql, object);
    }

    @Override
    public int updateShop(Shop shop) {
        String sql = "update shop set shopAddress = ?,shopCity = ?,shopDescribe = ?,shopDistrict = ?," +
                "shopImage = ?,shopLatitude=?,shopLongitude=?,shopName=?,shopPhone=?,shopProvince=? where shopId = ?";
        Object[] object = {
                shop.getShopAddress(),
                shop.getShopCity(),
                shop.getShopDescribe(),
                shop.getShopDistrict(),
                shop.getShopImage(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getShopName(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopId()
        };
        int row = JDBCDruidUtil.update(sql, object);
        return row;
    }

    @Override
    public int deleteShop(Integer shopId) {
        return 0;
    }

    @Override
    public Shop selectShop(Integer shopId) {

        return null;
    }

//    查询
    @Override
    public List<Shop> selectShops(Map<String, String> map) {
        List<Shop> list = new ArrayList<>();
        // 动态sql和动态数组
        StringBuffer sb = new StringBuffer("select * from shop where 1=1 ");
        List param = new ArrayList();
        // 判断name是否有数据，如果有，则拼接
        String name = map.get("keyword");
        if(name!=null && !"".equals(name)){
            sb.append(" and shopName like ? ");
            param.add("%"+name+"%");
        }
        // 判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime!=null && !"".equals(endTime)){
            sb.append(" and  createTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
//        判断手机号是否存在
        String phone = map.get("phone");
        if(phone != null && !"".equals(phone)){
            sb.append(" and shopPhone like ? ");
            param.add("%"+phone+"%");
        }
        //分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        sb.append(" order by shopId desc limit ?,? ");
        param.add( (pageNum-1)*pageSize );
        param.add(pageSize);

        String sql = sb.toString();
        Object object[] = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql,object);
        try {
            while (resultSet.next()){
                Shop shop = new Shop(
                        resultSet.getDate(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getInt(4),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getString(7),
                        resultSet.getString(8),
                        resultSet.getString(9),
                        resultSet.getString(10),
                        resultSet.getString(11),
                        resultSet.getString(12),
                        resultSet.getInt(13),
                        resultSet.getString(14),
                        resultSet.getString(15),
                        resultSet.getString(16),
                        resultSet.getString(17),
                        resultSet.getInt(18),
                        resultSet.getString(19),
                        resultSet.getString(20),
                        resultSet.getString(21),
                        resultSet.getString(22),
                        resultSet.getInt(23),
                        resultSet.getInt(24)
                );
                list.add(shop);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return list;
    }

//    查询总条数
    @Override
    public int selectShopCount(Map<String, String> map) {
        int count = 0;
// 动态sql和动态数组
        StringBuffer sb = new StringBuffer("select count(1) from shop where 1=1 ");
        List param = new ArrayList();
        // 判断name是否有数据，如果有，则拼接
        String name = map.get("keyword");
        if(name!=null && !"".equals(name)){
            sb.append(" and shopName like ? ");
            param.add("%"+name+"%");
        }
        // 判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime!=null && !"".equals(endTime)){
            sb.append(" and  createTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
//        判断手机号
        String phone = map.get("phone");
        if(phone != null && !"".equals(phone)){
            sb.append(" and shopPhone like ? ");
            param.add("%"+phone+"%");
        }

        String sql = sb.toString();
        Object object[] = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            if(resultSet.next()){
                count = resultSet.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        System.out.println(count);
        return count;
    }
}
